const { DataTypes } = require('sequelize');
const sequelize = require('../config/db');

const Product = sequelize.define('Product', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: '商品 ID'
    },
    name: {
        type: DataTypes.STRING(100),
        allowNull: false,
        defaultValue: '',
        comment: '商品名称'
    },
    category_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '分类 ID'
    },
    description: {
        type: DataTypes.TEXT,
        comment: '商品描述'
    },
    image_url: {
        type: DataTypes.STRING(255),
        defaultValue: '',
        comment: '商品主图'
    },
    banner_image_urls: {
        type: DataTypes.TEXT,
        comment: '商品图片集(JSON 格式存储多张图片 URL)'
    },
    rent_price_day: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: false,
        comment: '日租金额'
    },
    deposit: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: false,
        defaultValue: 0.00,
        comment: '押金'
    },
    min_rent_days: {
        type: DataTypes.INTEGER,
        defaultValue: 1,
        comment: '最少起租天数'
    },
    max_rent_days: {
        type: DataTypes.INTEGER,
        defaultValue: 365,
        comment: '最大租赁天数'
    },
    specifications: {
        type: DataTypes.TEXT,
        comment: '规格参数(JSON 格式)'
    },
    status: {
        type: DataTypes.TINYINT,
        allowNull: false,
        defaultValue: 1,
        comment: '状态:0 下架,1 上架'
    },
    view_num: {
        type: DataTypes.INTEGER,
        defaultValue: 0,
        comment: '浏览量'
    },
    is_recommend: {
        type: DataTypes.TINYINT(1),
        defaultValue: 0,
        comment: '是否推荐:0 否,1 是'
    },
    rent_date: {
        type: DataTypes.STRING(255),
        defaultValue: '',
        comment: '租期'
    },
    createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
        field: 'created_at',
        comment: '创建时间'
    },
    updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
        field: 'updated_at',
        comment: '更新时间'
    }
}, {
    tableName: 'product',
    timestamps: true,
    indexes: [
        {
            fields: ['category_id'],
            name: 'idx_category'
        },
        {
            fields: ['status'],
            name: 'idx_status'
        }
    ]
});

module.exports = Product;